import os
import sys
import subprocess

PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))

def generate_requirements():
    print("正在生成 requirements.txt ...")
    try:
        with open(os.path.join(PROJECT_DIR, 'requirements.txt'), 'w') as f:
            subprocess.check_call([sys.executable, '-m', 'pip', 'freeze'], stdout=f)
        print("requirements.txt 生成成功！")
    except Exception as e:
        print(f"生成 requirements.txt 失败: {e}")
        sys.exit(1)

def build_exe():
    print("正在打包为 exe 文件 ...")
    cmd = f"pyinstaller --onefile --noconsole --icon=./resources/logo2.ico --name=Xmind2TestCase main.py"
    result = os.system(cmd)
    if result == 0:
        print("打包完成！在dist目录中找到生成的.exe文件。")
    else:
        print("打包失败！请检查PyInstaller配置。")
        sys.exit(1)

if __name__ == "__main__":
    generate_requirements()
    build_exe()
